package net.cctms.zhishu.webAppFramework.ui.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RelativeLayout;


import net.cctms.zhishu.R;
import net.cctms.zhishu.webAppFramework.contact.UrlName;
import net.cctms.zhishu.webAppFramework.global.SwipeFinishActivity;
import net.cctms.zhishu.webAppFramework.utils.Log;
import net.cctms.zhishu.webAppFramework.video.NiceVideoPlayer;
import net.cctms.zhishu.webAppFramework.video.NiceVideoPlayerController;

import java.util.Map;

/**
 * 类名 VideoActivity
 * 作者 Keyu Chan
 * 创建日期 2017/9/5
 * 邮箱 chenky@corp.21cn.com
 * 说明 公共视频播放页面
 */
public class VideoActivity extends SwipeFinishActivity {

    protected NiceVideoPlayer mVideoPlayer;
    protected RelativeLayout mRootView;
    protected NiceVideoPlayerController mController;
    protected String mVideoUrl;
    protected String mVideoTitle;
 /*   public static final String VIDEO_URL = "video_url";
    protected static final String VIDEO_TITLE = "video_title";*/

    public static void openActivity(Context context, String videoUrl, String videoTitle) {
        Intent intent = new Intent(context, VideoActivity.class);
        intent.putExtra(UrlName.VIDEO_URL, videoUrl);
        intent.putExtra(UrlName.VIDEO_TITLE, videoTitle);

        if (context instanceof Activity) {
            context.startActivity(intent);
            ((Activity) context).overridePendingTransition(0, 0);
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        init();
    }

    protected void init() {
        initData();
        initView();
    }

    protected void initData() {
        Intent intent = getIntent();
        mVideoUrl = intent.getStringExtra(UrlName.VIDEO_URL);
        mVideoTitle = intent.getStringExtra(UrlName.VIDEO_TITLE);
    }

    protected void initView() {
        mRootView = (RelativeLayout) findViewById( R.id.video_root);
        initVideo();
    }

    protected void setUp(String url, Map<String, String> headers) {
        if (null == mVideoPlayer) {
            return;
        }
        mVideoPlayer.setUp(url, headers);
    }

    protected void initVideo() {
        mVideoPlayer = (NiceVideoPlayer) findViewById(R.id.video_player);
        mVideoPlayer.setUp(mVideoUrl, null);
        mController = new NiceVideoPlayerController(this);
        mController.setOnControllerListener(new ControllerListener());
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mRootView.addView(mController, params);
        mVideoPlayer.setController(mController);
        mVideoPlayer.post(new Runnable() {
            @Override
            public void run() {
                mVideoPlayer.start();
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        mController.onActivityPause();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mController.onActivityReStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mController.onActivityDestroy();
    }

    @Override
    public void onBackPressed() {
        if (mVideoPlayer.isFullScreen()) {
            mVideoPlayer.exitFullScreen();
            return;
        }
        finish();
        overridePendingTransition(0, 0);
    }

    protected boolean closeClick() {
        return false;
    }

    protected void shareClick() {
        Log.i("VideoActivity", "shareClick");
    }

    protected void playerError() {
        Log.i("VideoActivity", "playerError");
    }

    protected void playProgress(long position, long duration) {
        Log.i("VideoActivity", "playProgress");
    }

    class ControllerListener implements NiceVideoPlayerController.OnControllerListener {
        @Override
        public void onCloseClick() {
            if (!closeClick()) {
                finish();
                overridePendingTransition(0, 0);
            }
        }

        @Override
        public void onShareClick() {
            shareClick();
        }

        @Override
        public void onPlayerError() {
            playerError();
        }

        @Override
        public void onPlayProgress(long position, long duration) {
            playProgress(position, duration);
        }
    }

}
